﻿using Topshelf;
using EasyNetQ;
using Framework.Infrastructure.RabbitMQ;
using Framework.Infrastructure;
using Framework.Repository.Service;

namespace RabbitMQ.Subscriber
{
    public sealed class ServiceRunner : ServiceControl, ServiceSuspend
    {
        private readonly IBus bus;
        SYS_LoginLogService loginLogService = new SYS_LoginLogService();
        SYS_ApiVisitLogService apiVisitLogService = new SYS_ApiVisitLogService();
        SYS_OperationLogService operationLogService = new SYS_OperationLogService();
        SYS_ExceptionLogService exceptionLogService = new SYS_ExceptionLogService();

        private string host { get; set; }
        private string user { get; set; }
        private string pwd { get; set; }

        public ServiceRunner()
        {
            host = Common.GetAppSetting("rabbitmq_host");
            user = Common.GetAppSetting("rabbitmq_user");
            pwd = Common.GetAppSetting("rabbitmq_pwd");
            bus = RabbitHutch.CreateBus(string.Format("host={0};username={1};password={2}", host, user, pwd));
        }

        public bool Start(HostControl hostControl)
        {
            //NLogHelper.Debug("TopShelf开始...");
            bus.Subscribe<RabbitModel_Base>("MVCFramework_Queue", HandleAll);
            return true;
        }

        /// <summary>
        /// RabbitMQ消息订阅
        /// </summary>
        /// <param name="model"></param>
        private void HandleAll(RabbitModel_Base model)
        {
            //NLogHelper.Debug("进入消费者...");
            switch (model.ModelType)
            {
                case RabbitModel_Enum.LoginLog:
                    model.LoginLog.UserName = Common.MaxSubstring(model.LoginLog.UserName, 64);
                    if (!string.IsNullOrEmpty(model.LoginLog.UserPwd))
                        model.LoginLog.UserPwd = Common.MaxSubstring(model.LoginLog.UserPwd, 128);//用户输入内容需要处理，否则可能导致超长数据库报错
                    loginLogService.Add(model.LoginLog);
                    break;
                case RabbitModel_Enum.ApiVisitLog:
                    apiVisitLogService.AddVisitCount(model.ApiVisitLog);
                    break;
                case RabbitModel_Enum.OperationLog:
                    model.OperationLog.Params = Common.MaxSubstring(model.OperationLog.Params, 4096);
                    operationLogService.Add(model.OperationLog);
                    break;
                case RabbitModel_Enum.ExceptionLog:
                    model.ExceptionLog.Params = Common.MaxSubstring(model.ExceptionLog.Params, 4096);
                    model.ExceptionLog.Exception = Common.MaxSubstring(model.ExceptionLog.Exception, 4096);
                    exceptionLogService.Add(model.ExceptionLog);
                    break;

                default:
                    break;
            }
        }

        public bool Stop(HostControl hostControl)
        {
            bus.Dispose();
            return true;
        }

        public bool Continue(HostControl hostControl)
        {
            return true;
        }

        public bool Pause(HostControl hostControl)
        {
            return true;
        }
    }
}
